#include "widget.h"
#include "ui_widget.h"
#include <QKeyEvent>
#include <QDebug>
int timerid;
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    timerid = this->startTimer(1000); //以毫秒为单位

    connect(ui->pushButton, &mybutton::clicked,
            [=]()
    {
        qDebug() << "按键被按下";
    });
}

Widget::~Widget()
{
    delete ui;
}
/*
 * 按键按下事件
*/
void Widget::keyPressEvent(QKeyEvent *event)
{
    qDebug() << event->key();
    if(event->key() == Qt::Key_A)
    {
        qDebug() << "A 被按下";
    }
}
/*
 * 定时器事件
*/
void Widget::timerEvent(QTimerEvent *event)
{
    if(event->timerId() == timerid)
    {
        static int sec = 0;
        sec ++;
        ui->label->setText(QString("<center><h1>timer is:%1s</h1></centeer>").arg(sec));
        if(sec >= 10)
        {
            this->killTimer(timerid);
            qDebug("定时器停止");
        }
    }
}
